package edu.zjut.hjg.saas.kwmp.web.controller.teacher;

import static edu.zjut.hjg.saas.kwmp.web.support.KwmpConstants.SESSION_NAME_USER;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.service.BulletinService;
import edu.zjut.hjg.saas.common.annotation.CookieSessionIn;
import edu.zjut.hjg.saas.common.annotation.RequestAttribute;
import edu.zjut.hjg.saas.kwmp.web.model.UserSession;

@Controller
@RequestMapping("/teacher/bulletin")
public class BulletinManageController {

    @Autowired
    private BulletinService bulletinService;

    @RequestMapping("index.htm")
    public String index(@RequestAttribute Kindergarten app, Model model, boolean success) {
        model.addAttribute("bulletin", bulletinService.findByAppId(app.getId()));
        if (success) {
            model.addAttribute("message", "编辑成功");
        }
        return "teacher/bulletin";
    }

    @CookieSessionIn(value = SESSION_NAME_USER, param = "session")
    @RequestMapping(value = "edit.htm", method = RequestMethod.POST)
    public String edit(@RequestAttribute Kindergarten app, UserSession session, String content,
                       Model model) {
        bulletinService.edit(app.getId(), content, session.getId(), session.getLoginId());
        model.addAttribute("success", true);
        return "redirect:/teacher/bulletin/index.htm";
    }
}
